package main

import (
	"fmt"
	"time"
)

func main2() {

	DB.AutoMigrate(&Student{})

	//插入
	email := "xiaoming@163.com"
	ts, _ := time.Parse("2006-01-02 15:04:05", `2024-10-11 00:22:13`)
	student := Student{
		Name:     "小明",
		Age:      18,
		Email:    &email,
		Birthday: ts,
	}

	err := DB.Create(&student).Error
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("创建成功", student) //ID自动返回
	}

	fmt.Println("--------------")
	//批量插入 - 会返回ID
	var students []*Student
	for i := 0; i < 10; i++ {
		e := fmt.Sprintf("xiaoming%d@163.com", i)
		students = append(students, &Student{
			Name:     fmt.Sprintf("小明-%d", i),
			Age:      18 + i,
			Email:    &e,
			Birthday: ts,
		})
	}

	err = DB.Create(students).Error
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("创建成功", students) //ID自动返回
	}

	for _, s := range students {
		fmt.Println(*s)
	}
}
